package com.action.Employee;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.entity.Device;
import com.entity.Employee;
import com.entity.Parameter;
import com.entity.Substation;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.service.ParameterService;
import com.service.SubstationService;
import com.service.UserService;

public class LoginAction extends ActionSupport implements SessionAware{
	
	private SubstationService substationService;
	private ParameterService parameterService;
	private UserService userService;
	private Map session;
	private String userName;
	private String password;
	
	public SubstationService getSubstationService() {
		return substationService;
	}

	public void setSubstationService(SubstationService substationService) {
		this.substationService = substationService;
	}

	public ParameterService getParameterService() {
		return parameterService;
	}

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String findSubstation() throws Exception{
		List<Substation> Substationlist=new ArrayList<Substation>();	
		Substationlist = substationService.findAllSubstation();
		//Map session = ActionContext.getContext().getSession();
		session.put("substationlist" , Substationlist);
		findAllDeviceType();
		return SUCCESS;
	}
	
	public String findAllDeviceType() throws Exception{
		List<Parameter> Parameterlist=new ArrayList<Parameter>();
		Parameterlist = parameterService.findAllDeviceType();
		//Map session = ActionContext.getContext().getSession();
		session.put("parameterlist" , Parameterlist);
		return SUCCESS;
	}
	
	public String userLogin() throws Exception{
		List<Employee> Employeelist=new ArrayList<Employee>();
		Employeelist = userService.findUserByUserNameAndPasswoed(userName, password);
		if(Employeelist.size() == 0){
			return "fail";
		}else{
			if(Employeelist.get(0).getEmployeeAuthority() == 0){
				session.put("user" , "employee");
			}else{
				session.put("user" , "manager");
			}
			session.put("userName" , Employeelist.get(0).getEmployeeName());
			findSubstation();
			return "success";
		}
		//Map session = ActionContext.getContext().getSession();
	}
	
	public String loginOut() throws Exception{
			session.remove("user");
			session.remove("userName");
			return "success";
		}
		//Map session = ActionContext.getContext().getSession();

}
